package com.founder.bdyx.task;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.founder.bdyx.core.util.DateUtil;
import com.founder.bdyx.core.util.SpringContextHolder;
import com.founder.bdyx.modules.sys.controller.ApprovalController;
import com.founder.bdyx.modules.sys.mapper.ApplyInfoMapper;
import com.founder.bdyx.modules.sys.model.ApplyInfo;
import com.founder.bdyx.wxpro.conf.WxConfig;
import com.founder.bdyx.wxpro.util.HttpUtil;
import com.founder.bdyx.wxpro.util.VerifyUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

/**
 * 邮寄或自提通知定时任务
 */
@Component
public class SendOrPickUpNoticeTask implements Job {
    Log log = LogFactory.getLog(SendOrPickUpNoticeTask.class);

    @Autowired
    private WxConfig wxConfig;
    @Autowired
    private ApplyInfoMapper mapper;

    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {

        log.info("邮寄或自提通知。。");
        List<ApplyInfo> al = mapper.getAllSendOrPickUp("8");
        if (al == null || al.size() <= 0) return;

        String accessToken = VerifyUtil.getAccessToken(SpringContextHolder.getServletContext());
        log.info("accessToken : " + accessToken);

        for (ApplyInfo applyInfo : al){
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("touser",applyInfo.getUserId());

            String url = wxConfig.basePath + "/wxpro/redirect?index=4"+applyInfo.getUuid();
            jsonObject.put("url",url);

            JSONObject data = new JSONObject();

            if(applyInfo.getTransType().equals("1")){//预约自提
                jsonObject.put("template_id",wxConfig.sendNotice);
                JSONObject first = new JSONObject();//first
                first.put("value","您的病历已经打包完成！");
                first.put("color","#173177");
                JSONObject keyword1 = new JSONObject();//订单号
                keyword1.put("value",applyInfo.getUuid());
                keyword1.put("color","#173177");
                JSONObject keyword2 = new JSONObject();//自提码
                keyword2.put("value",applyInfo.getLogisticsNo());
                keyword2.put("color","#173177");
                JSONObject keyword3 = new JSONObject();//截止时间
                keyword3.put("value", DateUtil.parseDateAddOneDay(new Date(), "yyyy年MM月dd日 HH:mm"));//推迟一天
                keyword3.put("color","#173177");
                JSONObject remark = new JSONObject();//remark
                remark.put("value","请尽快到病案室领取！");
                remark.put("color","#173177");

                data.put("first",first);
                data.put("keyword1",keyword1);
                data.put("keyword2",keyword2);
                data.put("keyword3",keyword3);
                data.put("remark",remark);
            }else if (applyInfo.getTransType().equals("2")){//邮寄
                jsonObject.put("template_id",wxConfig.transNotice);

                JSONObject first = new JSONObject();//first
                first.put("value","您的病历已经打包邮寄!");
                first.put("color","#173177");
                JSONObject keyword1 = new JSONObject();//快递公司
                keyword1.put("value","顺丰快递");
                keyword1.put("color","#173177");
                JSONObject keyword2 = new JSONObject();//快递单号
                keyword2.put("value",applyInfo.getLogisticsNo());
                keyword2.put("color","#173177");
                JSONObject keyword3 = new JSONObject();//商品信息
                keyword3.put("value","病历档案");
                keyword3.put("color","#173177");
                JSONObject keyword4 = new JSONObject();//商品数量
                keyword4.put("value",applyInfo.getPrintCount() + "份");
                keyword4.put("color","#173177");
                JSONObject remark = new JSONObject();//remark
                remark.put("value","请注意查收!");
                remark.put("color","#173177");

                data.put("first",first);
                data.put("keyword1",keyword1);
                data.put("keyword2",keyword2);
                data.put("keyword3",keyword3);
                data.put("keyword4",keyword4);
                data.put("remark",remark);
            }
            jsonObject.put("data",data);

            String notice = HttpUtil.sendPost(wxConfig.templateUrl.replace("ACCESS_TOKEN",accessToken),jsonObject.toString());
            log.info("邮寄或自提通知模板消息返回参数：" + notice);

            JSONObject noticeJSON = JSON.parseObject(notice);
            if("0".equals(noticeJSON.getString("errcode"))){//消息通知成功后 更新状态
                applyInfo.setNoticeFlag("2");
                mapper.updatePayOrder(applyInfo);
            }
        }
    }
}
